home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_02 / numcvrt.c < prev    next >
C/C++ Source or Header  |  1991-05-15  |  10KB  |  392 lines

  1.  
  2.        /******************************************************
  3.        *
  4.        *       file d:\cips\intcvrt.c
  5.        *
  6.        *       Functions: This file contains
  7.        *           get_integer
  8.        *           int_convert
  9.        *
  10.        *       Purpose: These functions convert a string of
  11.        *                characters to their number value.
  12.        *
  13.        *       Modifications:
  14.        *            Taken from Jamsa's software package
  15.        *            and altered to fit into the computer
  16.        *            vision programming 22 August 1986.
  17.        *
  18.        *******************************************************/
  19.  
  20.  
  21. #include "d:\cips\numdefs.h"
  22.  
  23.  
  24. get_integer(n)
  25.    int *n;
  26. {
  27.    char string[80];
  28.  
  29.    read_string(string);
  30.    int_convert(string, n);
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  int_convert (ascii_val, result)
  38.     char *ascii_val;
  39.     int *result;
  40.   {
  41.     int sign = 1;  /* -1 if negative */
  42.  
  43.     *result = 0;   /* value returned to the calling routine */
  44.  
  45.     /* read passed blanks */
  46.  
  47.     while (is_blank(*ascii_val))
  48.        ascii_val++;              /* get next letter */
  49.  
  50.     /* check for sign */
  51.  
  52.     if (*ascii_val == '-' || *ascii_val == '+')
  53.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  54.  
  55.    /*
  56.     * convert the ASCII representation to the actual
  57.     * decimal value by subtracting '0' from each character.
  58.     *
  59.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  60.     * by subtracting '0' (or 48 in decimal) we get the desired
  61.     * value.
  62.     *
  63.     * if we have already converted '9' to 9 and the next character
  64.     * is '3', we must first multiply 9 by 10 and then convert '3'
  65.     * to decimal and add it to the previous total yielding 93.
  66.     *
  67.     */
  68.  
  69.     while (*ascii_val)
  70.      if (is_digit(*ascii_val))
  71.        *result = *result * 10 + to_decimal(*ascii_val++);
  72.  
  73.      else
  74.        return (IO_ERROR);
  75.  
  76.  
  77.     *result = *result * sign;
  78.  
  79.     return (NO_ERROR);
  80.   }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.          /************************************************
  87.          *
  88.          *         Functions: This file contains
  89.          *           get_short
  90.          *             short_convert
  91.          *
  92.          *         Purpose: These functions convert a string of
  93.          *                characters to their number value.
  94.          *
  95.          *         Modifications:
  96.          *            Taken from Jamsa's software package
  97.          *            and altered to fit into the computer
  98.          *            vision programming 22 August 1986.
  99.          *
  100.          *************************************************/
  101.  
  102.  
  103. get_short(n)
  104.    short *n;
  105. {
  106.    char string[80];
  107.  
  108.    read_string(string);
  109.    short_convert(string, n);
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  short_convert (ascii_val, result)
  117.     char *ascii_val;
  118.     short *result;
  119.   {
  120.     int sign = 1;  /* -1 if negative */
  121.  
  122.     *result = 0;   /* value returned to the calling routine */
  123.  
  124.     /* read passed blanks */
  125.  
  126.     while (is_blank(*ascii_val))
  127.        ascii_val++;              /* get next letter */
  128.  
  129.     /* check for sign */
  130.  
  131.     if (*ascii_val == '-' || *ascii_val == '+')
  132.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  133.  
  134.    /*
  135.     * convert the ASCII representation to the actual
  136.     * decimal value by subtracting '0' from each character.
  137.     *
  138.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  139.     * by subtracting '0' (or 48 in decimal) we get the desired
  140.     * value.
  141.     *
  142.     * if we have already converted '9' to 9 and the next character
  143.     * is '3', we must first multiply 9 by 10 and then convert '3'
  144.     * to decimal and add it to the previous total yielding 93.
  145.     *
  146.     */
  147.  
  148.     while (*ascii_val){
  149.      if (is_digit(*ascii_val)){
  150.        *result = *result * 10 + to_decimal(*ascii_val++);
  151.        if( (sign == -1) && (*result > 0)) *result = *result * -1;
  152.      }
  153.      else
  154.        return (IO_ERROR);
  155.     }  /* ends while ascii_val  */
  156.  
  157.     return (NO_ERROR);
  158.   }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.          /***********************************************
  165.          *
  166.          *         file d:\cips\locvrt.c
  167.          *
  168.          *         Functions: This file contains
  169.          *           get_long
  170.          *             long_convert
  171.          *
  172.          *         Purpose: These functions convert a string of
  173.          *                characters to their number value.
  174.          *
  175.          *         Modifications:
  176.          *            Taken from Jamsa's software package
  177.          *            and altered to fit into the computer
  178.          *            vision programming 22 August 1986.
  179.          *
  180.          ************************************************/
  181.  
  182.  
  183. get_long(n)
  184.    long *n;
  185. {
  186.    char string[80];
  187.  
  188.    read_string(string);
  189.    long_convert(string, n);
  190. }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  long_convert (ascii_val, result)
  197.     char *ascii_val;
  198.     long *result;
  199.   {
  200.     int sign = 1;  /* -1 if negative */
  201.  
  202.     *result = 0;   /* value returned to the calling routine */
  203.  
  204.     /* read passed blanks */
  205.  
  206.     while (is_blank(*ascii_val))
  207.        ascii_val++;              /* get next letter */
  208.  
  209.     /* check for sign */
  210.  
  211.     if (*ascii_val == '-' || *ascii_val == '+')
  212.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  213.  
  214.    /*
  215.     * convert the ASCII representation to the actual
  216.     * decimal value by subtracting '0' from each character.
  217.     *
  218.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  219.     * by subtracting '0' (or 48 in decimal) we get the desired
  220.     * value.
  221.     *
  222.     * if we have already converted '9' to 9 and the next character
  223.     * is '3', we must first multiply 9 by 10 and then convert '3'
  224.     * to decimal and add it to the previous total yielding 93.
  225.     *
  226.     */
  227.  
  228.     while (*ascii_val)
  229.      if (is_digit(*ascii_val))
  230.        *result = *result * 10 + to_decimal(*ascii_val++);
  231.      else
  232.        return (IO_ERROR);
  233.  
  234.  
  235.     *result = *result * sign;
  236.  
  237.     return (NO_ERROR);
  238.   }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.      /*******************************************************
  246.      *
  247.      *   file d:\cips\flocvrt.c
  248.      *
  249.      *   Functions: This file contains
  250.      *       get_float
  251.      *           float_convert
  252.      *       power
  253.      *
  254.      *   Purpose: This function converts a string of
  255.      *           characters to its number value.
  256.      *
  257.      *   Modifications:
  258.      *            This was taken from Jamsa's software
  259.      *            packages and modified to work in the
  260.      *            computer vision programs 22 August 1986.
  261.      *
  262.      *            16 June 1987 - the power function was not working
  263.      *                so Borland's Turbo C function pow10
  264.      *                was substituted for it.
  265.      *
  266.      ****************************************************/
  267.  
  268.  
  269. get_float(f)
  270.    float *f;
  271. {
  272.    char string[80];
  273.  
  274.    read_string(string);
  275.    float_convert(string, f);
  276. }
  277.  
  278.  float_convert (ascii_val, result)
  279.     char *ascii_val;
  280.     float *result;
  281.   {
  282.     int count;            /* # of digits to the right of the
  283.                              decimal point. */
  284.     int sign = 1;         /* -1 if negative */
  285.  
  286.     double pow10();       /* Turbo C function */
  287.     float power();        /* function returning a value raised
  288.                              to the power specified. */
  289.  
  290.     *result = 0.0;        /* value desired by the calling routine */
  291.  
  292.     /* read passed blanks */
  293.  
  294.     while (is_blank(*ascii_val))
  295.        ascii_val++;              /* get the next letter */
  296.  
  297.     /* check for a sign */
  298.  
  299.     if (*ascii_val == '-' || *ascii_val == '+')
  300.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  301.  
  302.  
  303.    /*
  304.     * first convert the numbers on the left of the decimal point.
  305.     *
  306.     * if the number is 33.141592  this loop will convert 33
  307.     *
  308.     * convert ASCII representation to the  actual decimal
  309.     * value by subtracting '0' from each character.
  310.     *
  311.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  312.     * by subtracting '0' (or 48 in decimal) we get the desired
  313.     * value.
  314.     *
  315.     * if we have already converted '9' to 9 and the next character
  316.     * is '3', we must first multiply